package com.leancloud.circle.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.aries.ui.view.title.TitleBarView;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.leancloud.circle.R;
import com.leancloud.circle.manage.CirclePathManage;

import java.util.ArrayList;

import tech.com.commoncore.base.BaseTitleActivity;

import static com.leancloud.circle.manage.CirclePathManage.CIRCLE_PUBLISH_ARTICLE;

/**
 * 圈子界面
 */
@Route(path = CirclePathManage.CIRCLE_ARTICLE_LIST)
public class CircleActivity extends BaseTitleActivity {

    private final static String[] title = {"推荐", "关注"};

    private SegmentTabLayout segmentTabLayout;
    private FragmentManager mFragmentManager;
    private FragmentTransaction mTransaction;

    private ArrayList<Fragment> fragments;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText("")
                .setRightTextDrawable(getResources().getDrawable(R.mipmap.circle_icon_add))
                .setOnRightTextClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ARouter.getInstance().build(CIRCLE_PUBLISH_ARTICLE).navigation();
                    }
                });
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_circle;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        segmentTabLayout = mContentView.findViewById(R.id.circle_tab_layout);

        segmentTabLayout.setTabData(title);
        segmentTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                selectFragment(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        mFragmentManager = getSupportFragmentManager();
        buildAllFragment();
    }

    private void buildAllFragment() {
        fragments = new ArrayList<>();
        selectFragment(0);
    }

    private Fragment createFragment(Fragment fragment) {
        if (fragment == null) {
            return null;
        }
        mTransaction = mFragmentManager.beginTransaction();
        mTransaction.add(R.id.circle_find_fragment, fragment);
        mTransaction.commit();
        return fragment;
    }

    private void selectFragment(int position) {
        mTransaction = mFragmentManager.beginTransaction();
        hideUnSelectFragment(mTransaction);
        if (position > fragments.size()) {
            return;
        }
        Fragment selectFragment = fragments.get(position);
        if (selectFragment != null) {
            mTransaction.show(selectFragment);
        }
        mTransaction.commit();
    }

    private void hideUnSelectFragment(FragmentTransaction mTransaction) {
        for (Fragment unSelectFragment : fragments) {
            if (unSelectFragment != null) {
                mTransaction.hide(unSelectFragment);
            }
        }
    }
}
